package com.aat.dizena.user.web.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aat.dizena.base.bean.User;
import com.aat.dizena.base.service.UserService;
import com.aat.dizena.base.web.controller.BaseController;
import com.aat.dizena.utils.SecurityTools;
import com.aat.dxfy.utils.Atools;

/**
 * @time 2016-05-25
 * @author 新歌<xingeking@163.com>
 * @email xingeking@163.com
 * @version v1.0
 * @function 首页管理
 * @info java spring aop
 * @update none
 */

@Controller
@Scope("prototype")
@RequestMapping("user")
public class IndexUserController extends BaseController {

	private String pwdupt = "/base/pwdupt";
	private String loginjsp = "redirect:/base/login.do";
	private String logoutJsp = "redirect:/base/logout.do";
	private String pwdfindElink = "/base/pwdfindElink";
	private String pwdfindEpcode = "/base/pwdfindEpcode";

	@Autowired
	private UserService userService;

	// 1，检测用户/邮箱/phone是否已经注册接口;json
	@RequestMapping(value = "chkEPU.do")
	@ResponseBody
	public int chkEPU(@RequestParam(value = "epu", defaultValue = "~") String epu) {
		int i = 0;
		if (isNotEmpty(epu)) {
			User user = userService.queryUserLogin(epu);
			if (user == null) {
				i = 0;
			} else {
				i = 1;
			}
		}
		return i;
	}

	// 2，用户修改密码处理；
	@RequestMapping(value = "pwdupt.do", method = RequestMethod.GET)
	public String pwdupt(Model m) {
		if (getSessionUser() == null) {
			return loginjsp;
		}
		return pwdupt;
	}

	@RequestMapping(value = "pwdupt.do", method = RequestMethod.POST)
	public String pwdupt(@RequestParam(value = "p", defaultValue = "1") String p,
			@RequestParam(value = "p1", defaultValue = "1") String p1,
			@RequestParam(value = "p2", defaultValue = "1") String p2, Model m) {
		User user = getSessionUser();
		if (user == null) {
			return loginjsp;
		} else {
			String salt = user.getSalt();
			String pwd = Atools.makePwd(p + salt);// 加密
			if (user.getPasswd().equals(pwd)) {
				if (isEmpty(p1) || p1.length() < 6 || p1.length() > 20) {
					m.addAttribute("error", "新密码6-20位");
					return pwdupt;
				}
				if (!p1.equals(p2)) {
					m.addAttribute("error", "两次密码输入不一致");
					return pwdupt;
				}
				String saltNew = Atools.jiami(p1);
				String pwdNew = Atools.makePwd(p1 + saltNew);// 加密
				User tmp = new User();
				tmp.setId(user.getId());
				tmp.setSalt(saltNew);
				tmp.setPasswd(pwdNew);
				userService.updateBean(tmp);
			} else {
				m.addAttribute("error", "原密码错误");
				return pwdupt;
			}
		}
		return logoutJsp;
	}

	// 3，用户找回密码处理-设置新的密码；
	@RequestMapping(value = "pwdfindElink.do", method = RequestMethod.GET)
	public String pwdfindElink(@RequestParam("sid") String sid, @RequestParam("dt") String dt, Model m) {

		long l1 = new Date().getTime();
		long l2 = 0L;
		try {
			SecurityTools st = new SecurityTools("dizena123");
			String dt1 = st.jiemi(dt);
			Date dt2 = Atools.getDateFmtStr(dt1, "yyyy-MM-dd HH:mm:ss");
			l2 = dt2.getTime();
			long l3 = l1 - l2;
			if (l3 < 1800 * 1000) {
				m.addAttribute("sid", sid);
				m.addAttribute("dt", dt);
				return pwdfindElink;
			} else {
				m.addAttribute("error", "您的邮箱验证信息已经过期了");
				return loginjsp;
			}
		} catch (Exception e) {

		}
		m.addAttribute("error", "您的邮箱验证信息已经过期了");
		return loginjsp;

	}

	@RequestMapping(value = "pwdfindElink.do", method = RequestMethod.POST)
	public String pwdfindElink(@RequestParam("sid") String sid, @RequestParam("dt") String dt,
			@RequestParam(value = "p1", defaultValue = "1") String p1,
			@RequestParam(value = "p2", defaultValue = "1") String p2, Model m) {
		SecurityTools st = new SecurityTools("dizena123");
		try {
			long l1 = new Date().getTime();
			long l2 = 0L;
			String dt1 = st.jiemi(dt);
			Date dt2 = Atools.getDateFmtStr(dt1, "yyyy-MM-dd HH:mm:ss");
			l2 = dt2.getTime();
			long l3 = l1 - l2;
			if (l3 < 1800 * 1000) {
				String ep = st.jiemi(sid);
				User user = userService.queryUserLogin(ep);
				if (user == null) {
					m.addAttribute("sid", sid);
					m.addAttribute("dt", dt);
					m.addAttribute("error", "找回密码失败请联系工作人员！");
					return pwdfindElink;
				} else {
					if (isEmpty(p1) || p1.length() < 6 || p1.length() > 20) {
						m.addAttribute("sid", sid);
						m.addAttribute("dt", dt);
						m.addAttribute("error", "新密码6-20位");
						return pwdfindElink;
					}
					if (!p1.equals(p2)) {
						m.addAttribute("sid", sid);
						m.addAttribute("dt", dt);
						m.addAttribute("error", "两次密码输入不一致");
						return pwdfindElink;
					}

					String saltNew = Atools.jiami(p1);
					String pwdNew = Atools.makePwd(p1 + saltNew);// 加密
					User tmp = new User();
					tmp.setId(user.getId());
					tmp.setSalt(saltNew);
					tmp.setPasswd(pwdNew);
					userService.updateBean(tmp);
					//
					return loginjsp;
				}
			} else {
				m.addAttribute("error", "您的邮箱验证信息已经过期了");
				return loginjsp;
			}
		} catch (Exception e) {

		}
		m.addAttribute("error", "您的邮箱验证信息已经过期了");
		return loginjsp;

	}

	// 4，验证码更新密码；ep,验证码,密码1,2； 要不要分步骤，一次性；
	@RequestMapping(value = "pwdfindEpcode.do", method = RequestMethod.POST)
	public String pwdfindEpcode(@RequestParam(value = "ep", defaultValue = "1") String ep,
			@RequestParam(value = "c", defaultValue = "1") String xcode,
			@RequestParam(value = "p1", defaultValue = "1") String p1,
			@RequestParam(value = "p2", defaultValue = "1") String p2, Model m) {
		Object epo = getSession().getAttribute(ep);
		if (epo != null) {
			String code = (String) epo;
			if (code.equals(xcode)) {
				User user = userService.queryUserLogin(ep);
				if (user != null) {
					if (isEmpty(p1) || p1.length() < 6 || p1.length() > 20) {
						m.addAttribute("error", "新密码6-20位");
					}
					if (!p1.equals(p2)) {
						m.addAttribute("error", "两次密码输入不一致");
					}
					String saltNew = Atools.jiami(p1);
					String pwdNew = Atools.makePwd(p1 + saltNew);// 加密
					User tmp = new User();
					tmp.setId(user.getId());
					tmp.setSalt(saltNew);
					tmp.setPasswd(pwdNew);
					userService.updateBean(tmp);
					//
					return loginjsp;

				} else {
					m.addAttribute("error", "账户不存在");
					m.addAttribute("ep", ep);
					return pwdfindEpcode;
				}

			} else {
				m.addAttribute("error", "验证码输入错误");
				m.addAttribute("ep", ep);
				return pwdfindEpcode;
			}
		} else {
			m.addAttribute("error", "账户错误");
			m.addAttribute("ep", ep);
			return pwdfindEpcode;
		}

	}

	// end
}
